<?php

namespace app\controller;



use app\service\CastleLevelConfigService;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

//城堡等级配置控制器
class CastleLevelConfigController extends Base
{

    /**
     * 获取城堡等级配置
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function index(): Response
    {
        $res = (new CastleLevelConfigService())->get_config_by_lv($this->user_id);
        return $this->json($res);
    }


    //升级城堡
    public function upgrade(): Response
    {

        // 启动事务
        Db::startTrans();
        try {
            $res = (new CastleLevelConfigService())->upgrade($this->user_id);

            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollBack();
            return $this->fail("服务器异常".$e->getLine());
        }

    }


}